laravel 5.3 in openshift

雖然 openshift 有提供的 Laravel 卡匣(cartridge),但版本只到 5.0 版本,環境也只有 PHP 5.4,無法滿足 Laravel 5.3 的最低需求 PHP >= 5.6.4 。所以這邊將記錄如何在 openshift 安裝 PHP 5.6.4 以上版本環境。

PHP 7.0 + MySQL5.5 的環境

(1) 安裝 nginx 卡匣

官方 github:

https://github.com/boekkooi/openshift-cartridge-nginx

卡匣位置:

http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-nginx

(2) 安裝 PHP 卡匣

官方 github:

https://github.com/boekkooi/openshift-cartridge-php

卡匣位置:

http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-php

(3) 安裝 Openshfit 提供的官方 MySQL 5.5 卡匣

這邊直接使用官方提供的 MySQL 5.5 卡匣,安裝完畢後,會有登入的帳號密碼、資料庫名稱,抄錄下來,等會兒會使用到。

(4) git clone 專案檔

透過 git 將專案檔 clone 到本機,在這個動作之前,要先到 openshift 的後台,將自己的 ssh-key 登錄進去

(5) 修改 nginx 設定

.openshift/nginx.conf.erb 的內容,找到該部分

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;
    root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;

    location / {
        index  index.html index.htm;
    }
}

修改為

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;
    root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;

    index index.html index.htm index.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:<%= ENV['OPENSHIFT_PHP_SOCKET'] %>;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_script_name;
      include <%= ENV['OPENSHIFT_NGINX_DIR'] %>/usr/nginx-<%= ENV['OPENSHIFT_NGINX_VERSION'] %>/conf/fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

(6) 建立 openshift 的事件腳本

建立 .openshift/action_hooks/build 檔案,內容為

1
2
3
4
5
6
#!/bin/bash
set -e

# Build PHP extra's
# Update conf/ini files and install pecl extensions
${OPENSHIFT_PHP_DIR}/bin/control build

設定執行權限 chmod +x .openshift/action_hooks/build 若是在 window 你必需透過 git update-index --chmod=+x .openshift/action_hooks/build 來設定。這個腳本是為了 openshift 在 build 時會去試著找 .openshift/fpm 和 .openshift/cli 的設定檔來覆寫原本 php.ini 或 nginx 的設定檔

(7) 使用自訂設定檔修正 MySQL 的 socket 路徑設定錯誤的問題

從這邊 https://github.com/boekkooi/openshift-cartridge-php/tree/master/conf 下載三個設定檔

  • php.ini.erb 下載至 .openshift/cli/php.ini.erb
  • php-fpm.ini.erb 下載至 .openshift/fpm/php.ini.erb
  • php-fpm.conf.erb 下載至 .openshift/fpm/php-fpm.conf.erb

.openshift/cli/php.ini.erb.openshift/fpm/php.ini.erb 設定檔內容,搜尋三個屬性

pdo_mysql.default_socket =
mysql.default_socket =
mysqli.default_socket =

設定如下:

pdo_mysql.default_socket = <%= defined?(ENV['OPENSHIFT_MYSQL_DB_SOCKET']) ? ENV['OPENSHIFT_MYSQL_DB_SOCKET'] : '' %>
mysql.default_socket = <%= defined?(ENV['OPENSHIFT_MYSQL_DB_SOCKET']) ? ENV['OPENSHIFT_MYSQL_DB_SOCKET'] : '' %>
mysqli.default_socket = <%= defined?(ENV['OPENSHIFT_MYSQL_DB_SOCKET']) ? ENV['OPENSHIFT_MYSQL_DB_SOCKET'] : '' %>

.openshift/fpm/php-fpm.conf.erb 設定檔內容找到

;Informational Environment Variables

追加缺少的環境變數

env[OPENSHIFT_SECRET_TOKEN] = $OPENSHIFT_SECRET_TOKEN

(8) 測試資料庫連線

接著下載 adminer.php 至 public/adminer.php。然後 git push 專案檔後,開啟 http://{app-name}-{username}.rhcloud.com 輸入步驟(3)所抄錄的登入帳號密碼,進行資料庫連線測試

Laravel 5.3 安裝

(1) 在本地端透過 composer 安裝好 laravel 5.2

先在本地端透過 composer create-project –prefer-dist laravel/laravel blog 安裝 laravel 5.3 後,再把整個 blog 的內容移動覆寫到 openshift 專案資料夾

(2) 修改 laravel 設定檔

config/app.php

[
    'url' => env('APP_URL', env('OPENSHIFT_APP_DNS', 'http://localhost')),
    'key' => env('APP_KEY', env('OPENSHIFT_SECRET_TOKEN')),
]

config/database.php

'connections' => [
    'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path().'/database.sqlite3',
        'prefix'   => '',
    ],
    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', env('OPENSHIFT_MYSQL_DB_HOST', 'localhost')),
        'port'      => env('DB_PORT', env('OPENSHIFT_MYSQL_DB_PORT', 3306)),
        'database'  => env('DB_DATABASE', env('OPENSHIFT_APP_NAME', 'forge')),
        'username'  => env('DB_USERNAME', env('OPENSHIFT_MYSQL_DB_USERNAME', 'forge')),
        'password'  => env('DB_PASSWORD', env('OPENSHIFT_MYSQL_DB_PASSWORD', '')),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
    'pgsql' => [
        'driver'   => 'pgsql',
        'host'     => env('DB_HOST', env('OPENSHIFT_POSTGRESQL_DB_HOST', 'localhost')),
        'port'     => env('DB_PORT', env('OPENSHIFT_POSTGRESQL_DB_PORT', 5432)),
        'database' => env('DB_DATABASE', env('OPENSHIFT_APP_NAME', 'forge')),
        'username' => env('DB_USERNAME', env('OPENSHIFT_POSTGRESQL_DB_USERNAME', 'forge')),
        'password' => env('DB_PASSWORD', env('OPENSHIFT_POSTGRESQL_DB_PASSWORD', '')),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ]
],
// ... 略 ...

(3) 追加 for laravel 的事件腳本內容

新增並編輯 .openshift/action_hooks/deploy 檔案,注意!不是 build ,在 build 的事件腳本是無法對資料庫進行連線,這會導致 php artisan migrate 的錯誤。設定執行權限 chmod +x .openshift/action_hooks/deploy 若是在 window 你必需透過 git update-index --chmod=+x .openshift/action_hooks/deploy 來設定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
( cd $OPENSHIFT_REPO_DIR ; composer install --no-interaction --no-dev )
source $OPENSHIFT_CARTRIDGE_SDK_BASH

set -e

echo 1>&2
echo "Setting environment variables..." 1>&2

export APP_ENV="${APPLICATION_ENV:-production}"
if [[ "${APPLICATION_ENV:-}" != "development" ]]; then
export APP_DEBUG=false
else
export APP_DEBUG=true
fi
if [ "$(type -t set_env_var)" == "function" ]; then
set_env_var 'APP_ENV' $APP_ENV $OPENSHIFT_HOMEDIR/.env/user_vars
set_env_var 'APP_DEBUG' $APP_DEBUG $OPENSHIFT_HOMEDIR/.env/user_vars
fi


if [ ! -z "$OPENSHIFT_APP_DNS" ]; then
export APP_URL="$OPENSHIFT_APP_DNS"
if [ "$(type -t set_env_var)" == "function" ]; then
set_env_var 'APP_URL' $APP_URL $OPENSHIFT_HOMEDIR/.env/user_vars
fi
fi

if [ ! -z "$OPENSHIFT_SECRET_TOKEN" ]; then
export APP_KEY=${OPENSHIFT_SECRET_TOKEN:0:31}
if [ "$(type -t set_env_var)" == "function" ]; then
set_env_var 'APP_KEY' $APP_KEY $OPENSHIFT_HOMEDIR/.env/user_vars
fi
fi

if [ ! -z "$OPENSHIFT_POSTGRESQL_DB_HOST" ]; then
export DB_DRIVER="pgsql"
fi

if [ ! -z "$OPENSHIFT_MYSQL_DB_HOST" ]; then
export DB_DRIVER="mysql"
fi

if [ -z "$DB_DRIVER" ]; then
echo 1>&2
echo "Could not find the database cartridge. Please run:" 1>&2
echo "rhc cartridge add -a $OPENSHIFT_APP_NAME -c mysql-5.5" 1>&2
echo "or"
echo "rhc cartridge add -a $OPENSHIFT_APP_NAME -c postgresql-9.2" 1>&2
echo "then make a sample commit (add whitespace somewhere) and re-push" 1>&2
echo 1>&2
exit 5
else
echo 1>&2
echo "Database driver set to '$DB_DRIVER'..." 1>&2
echo 1>&2
if [ "$(type -t set_env_var)" == "function" ]; then
set_env_var 'DB_DRIVER' $DB_DRIVER $OPENSHIFT_HOMEDIR/.env/user_vars
fi
fi

echo 1>&2
echo "Migrating storage and log directories..." 1>&2
echo 1>&2
# If this is initial installation, copy the default storage directory
# from official Laravel installation and remove the log directory
#
if [ ! -d ${OPENSHIFT_DATA_DIR}storage ]; then
mv ${OPENSHIFT_REPO_DIR}storage ${OPENSHIFT_DATA_DIR}storage
rm -rf ${OPENSHIFT_DATA_DIR}storage/logs
fi

# Create symlink to new storage directory location
ln -sf ${OPENSHIFT_DATA_DIR}storage ${OPENSHIFT_REPO_DIR}storage

# Create a symlink to the new log directory location
ln -sf ${OPENSHIFT_LOG_DIR} ${OPENSHIFT_DATA_DIR}storage/logs

# Use repository .env
if [ -f ${OPENSHIFT_REPO_DIR}.openshift/.env ]; then
cp -f ${OPENSHIFT_REPO_DIR}.openshift/.env ${OPENSHIFT_REPO_DIR}/.env
fi


echo 1>&2
echo "Running 'php artisan migrate --force'..." 1>&2
echo 1>&2

pushd ${OPENSHIFT_REPO_DIR} > /dev/null
php artisan migrate --force
popd > /dev/null

目錄

Go Top